package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.KachcheryDAO;
import lk.icta.core.dao.ScheduleDAO;
import lk.icta.core.entity.Kachchery;
import lk.icta.core.entity.Schedule;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * @author Kusal Samarakoon
 */
public class KachcheryDAOImpl extends BaseDAO implements KachcheryDAO {

    @Transactional(propagation = Propagation.MANDATORY)
    public void addKachchery(Kachchery kachchery, User user) {
        logger.debug("Adding a Kachchery {}", kachchery.getId());
        kachchery.getLifeCycleInfo().setCreatedTimeStamp(new Date());
        kachchery.getLifeCycleInfo().setCreatedUser(user);
        kachchery.getLifeCycleInfo().setLastUpdatedTimeStamp(new Date());
        kachchery.getLifeCycleInfo().setLastUpdatedUser(user);
        em.persist(kachchery);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateKachchery(Kachchery kachchery, User user) {
        logger.debug("Updating a kachchery {}", kachchery.getId());
        kachchery.getLifeCycleInfo().setLastUpdatedTimeStamp(new Date());
        kachchery.getLifeCycleInfo().setLastUpdatedUser(user);
        em.merge(kachchery);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<Kachchery> getAllKachcheryByState(Kachchery.Status status) {
        Query q = em.createNamedQuery("getAllKachcheryByState");
        q.setParameter("status", status);
        return q.getResultList();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<Kachchery> getAllKachcheryByDSDivisionAndState(Kachchery.Status status, User user) {
        Query q = em.createNamedQuery("getAllKachcheryByDSDivisionAndState");
        q.setParameter("status", status);
        //q.setParameter("dsDivisionId", dsDivisionId);

        populateDsDivisionList(q, user);
        return q.getResultList();
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Kachchery getKachcheryById(long id) {
        logger.debug("Loading Kachchery [Kachchery Id: {}]", id);
        return em.find(Kachchery.class, id);
    }

    private void populateDsDivisionList(Query q, User user) {

        if (user != null && user.getDsDivisions() != null && user.getDsDivisions().size() != 0) {
            //for orlp and lcg, find all apps, ignore DS filter
            logger.debug("ds div list size : {}", user.getDsDivisions().size());
            q.setParameter("ignoreDS", 0);
            q.setParameter("DSList", user.getDsDivisions());

        } else {
            q.setParameter("ignoreDS", 1);
            q.setParameter("DSList", null);
        }

    }


}
